import { link } from 'fs';
import spider from '../spider';
const cherrio = require('cheerio');
const download = require('download');
let url: string = 'http://www.bhu.edu.cn';
let linkArr: any = [];
let spiderInstance1 = new spider({
  url,
  callback: function (html: any) {
    console.log('网站获取成功：', html);
    const $: cheerio.Root = cherrio.load(html);
    let links = $('link');
    links.each((idx, item) => {
      item && linkArr.push(encodeURI(url +(<any>item).attribs.href));
    });
    console.log(linkArr);
    Promise.all(linkArr.map((v: any) => download(v, 'links'))).then(_ => {
      console.log('下载成功！');
    })
  }
});
spiderInstance1.start();